package com.chenfan.mcn.controller;

import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.annotations.ActionTracing;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.extension.Res;
import com.chenfan.common.extension.validation.groups.Add;
import com.chenfan.mcn.model.CommentLog;
import com.chenfan.mcn.model.dto.CommentAddDTO;
import com.chenfan.mcn.model.dto.CommentUpdateDTO;
import com.chenfan.mcn.service.CommentLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单据评论
 * @author:   tangwei
 * Date:     2021/4/16 14:27
 * Description: 单据评论
 */
@RestController
@RequestMapping("/commentLog")
public class CommentLogController {

    @Autowired
    private CommentLogService commentLogService;

    /**
     * 新增评论
     * @param commentAddDTO
     * @return
     */
    @ActionTracing("评论")
    @PostMapping("/add")
    public Res<Boolean> addComment(@Validated({Add.class}) @RequestBody CommentAddDTO commentAddDTO) {
        return Res.ok(commentLogService.addComment(commentAddDTO));
    }

    /**
     * 评论列表
     * @param businessId
     * @param businessType
     * @return
     */
    @GetMapping("/list")
    public Res<List<CommentLog>> getCommentList(@RequestParam Long businessId, @RequestParam Integer businessType) {
        if (businessId == null || businessType == null) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return Res.ok(commentLogService.getCommentList(businessId, businessType));
    }

    /**
     * 编辑评论
     * @param commentUpdateDTO
     * @return
     */
    @ActionTracing("编辑")
    @PostMapping("/update")
    public Res<Boolean> updateComment(@Validated @RequestBody CommentUpdateDTO commentUpdateDTO) {
        return Res.ok(commentLogService.updateComment(commentUpdateDTO));
    }

    /**
     * 评论详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Res<CommentLog> commentDetail(@RequestParam Long id) {
        if (id == null) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return Res.ok(commentLogService.commentDetail(id));
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @ActionTracing("删除")
    @GetMapping("/delete")
    public Res<Boolean> deleteComment(@RequestParam Long id) {
        if (id == null) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return Res.ok(commentLogService.deleteComment(id));
    }

}